package com.sskj.finance.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.finance.bean.AccountBean;
import com.sskj.finance.bean.HomeBorrowGetTransrecordBean;
import com.sskj.finance.bean.HomeLendGetTransrecord;
import com.sskj.finance.bean.RateBean;
import com.sskj.finance.bean.SupplyData;
import com.sskj.finance.http.HttpConfig;
import com.sskj.finance.ui.fragment.FinanceFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;


public class FinanceFragmentPresenter extends BasePresenter<FinanceFragment> {


    public void getRate(String tran_id) {
        httpService.getFinanceRate(tran_id)
                .execute(new JsonCallBack<HttpData<RateBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpData<RateBean>> response) {
                        HttpData<RateBean> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.showCancel(httpData.getData());
                        }
                    }
                });
    }



    /**
     * 理财记录
     *
     * @param status 1理财中 2已赎回
     * @param p      页码，默认1
     * @param size   页条数，默认10
     * @return
     */
    public Flowable<List<HomeLendGetTransrecord>> homeLendGetTransrecord(String status, String p, String size) {
        return httpService.homeLendGetTransrecord(status, p, size)
                .converter(new JsonConverter<HttpData<PageBean<HomeLendGetTransrecord>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());
    }

    /**
     * 理财赎回
     *
     * @param order_id 理财记录id
     * @return
     */
    public void homeLendPayBack(String order_id,String tpwd) {
        httpService.homeLendPayBack(order_id,tpwd).execute(new JsonCallBack<HttpData>(this) {
            @Override
            public void onSuccess(Response<HttpData> response) {
                HttpData httpData = response.body();
                if (httpData.getStatus() == HttpConfig.OK) {

                    mView.backSuccess();
                }
            }
        });
    }

}
